package com.os.sys.controller;

import com.os.sys.common.utility.PubClass;
import com.os.sys.common.utility.XJsonInfo;
import com.os.sys.model.T_SYS_Param;
import com.os.sys.repository.ParamRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;

/**
 * Created by Administrator on 2016/12/13.
 */

@RestController
@RequestMapping("/api/sys/param")
public class ParamController {
    @Resource
    private ParamRepository paramRepository;
    @Resource
    private PubClass pubClass;

    @RequestMapping(value = "/getModel")
    public XJsonInfo getModel(Long id) {
        T_SYS_Param info = paramRepository.findById(id).orElse(null);
        return new XJsonInfo().setData(info);
    }
    @RequestMapping(value = "/getDataValue")
    public XJsonInfo getDataValue(String paramKey) {
        List<T_SYS_Param> list = paramRepository.findDataValue(paramKey);
        return new XJsonInfo().setData(list);
    }

    @RequestMapping(value = "/getDataValueForUser")
    public XJsonInfo getDataValueForUser(String paramKey) {
        List<T_SYS_Param> list = paramRepository.findDataValueForUser(paramKey);
        return new XJsonInfo().setData(list);
    }

    @RequestMapping(value = "/getTopList")
    public XJsonInfo getTopList() {
        List<T_SYS_Param> listMenu = paramRepository.findByParentId(0l);

        List<T_SYS_Param> endList = createTree(listMenu, 0l);
        return new XJsonInfo().setData(endList);
    }

    @RequestMapping(value = "/getTreeForAdmin")
    public XJsonInfo getTreeForAdmin() {
        List<T_SYS_Param> listMenu = (List<T_SYS_Param>) paramRepository.findAll();

        List<T_SYS_Param> endList = createTree(listMenu, 0l);
        return new XJsonInfo().setData(endList);
    }

    @RequestMapping(value = "/getTreeForUser")
    public XJsonInfo getTreeForUser() {

        List<T_SYS_Param> listMenu = paramRepository.findByEnable(1);

        List<T_SYS_Param> endList = createTree(listMenu, 0l);
        return new XJsonInfo().setData(endList);
    }

    @Transactional
    @RequestMapping(value = "/saveTopData", method = RequestMethod.POST)
    public XJsonInfo saveTopData(@RequestBody T_SYS_Param info) {
//        if(info.getId()>0){
//            T_SYS_Param dbInfo=paramRepository.findOne(info.getId());
//            if(!Objects.equals(dbInfo.getDataType(), info.getDataType())){ // 删除所有孩子
//                paramRepository.deleteDataValues(info.getKey());
//            }
//        }
        paramRepository.save(info);

        return new XJsonInfo();
    }

    @RequestMapping(value = "/saveText", method = RequestMethod.POST)
    public XJsonInfo saveText(@RequestBody T_SYS_Param info) {
        info.setParentId(100l);
        paramRepository.save(info);
        return new XJsonInfo();
    }
    @RequestMapping(value = "/saveCombobox", method = RequestMethod.POST)
    public XJsonInfo saveCombobox(@RequestBody T_SYS_Param info) {
        info.setParentId(100l);
        paramRepository.save(info);
        return new XJsonInfo();
    }

    @RequestMapping(value = "/addCombobox", method = RequestMethod.POST)
    public XJsonInfo addCombobox(@RequestBody T_SYS_Param info) {
        info.setId(null);
        info.setParentId(100l);
        paramRepository.save(info);
        return new XJsonInfo();
    }



    @RequestMapping(value = "/removeCombobox", method = RequestMethod.DELETE)
    public XJsonInfo removeCombobox(Long id) {
        paramRepository.deleteById(id);
        return new XJsonInfo();
    }

    private List<T_SYS_Param> createTree(List<T_SYS_Param> dbData, Long parentId) {
        List<T_SYS_Param> childrenList = dbData.stream().filter(q -> Objects.equals(q.getParentId(), parentId)).sorted(comparing(T_SYS_Param::getOrderIndex).reversed()).collect(Collectors.toList());
        if (childrenList.size() == 0) {
            return null;
        }

        for (T_SYS_Param source : childrenList) {
            source.setChildren(createTree(dbData, source.getId()));
        }
        return childrenList;
    }
}
